<?php
require_once "Class/database.php";
require_once "Class/channelService.php";
/**
*
*/
class RSSFeed{
    private $tedId = 15;
    private $tedUrl = "http://feeds.feedburner.com/tedtalks_video";
    private $channelService = null;
    function getFeed($feed_url) {

        $content = file_get_contents($feed_url);
        $x = new SimpleXmlElement($content);

        $itemArray  = array();

        foreach($x->channel->item as $entry) {
            $itemArray[] = array(
                'Name' => (string)$entry->title,
                'Link'=>(string)$entry->enclosure->attributes()->url
            );
        }
        return $itemArray;
    }
    public function AutoUpdateFeedData(){
        $tedsChannel = $this->getFeed($this->tedUrl);
        $this->channelService = new channelService();
        $currentServers = $this->channelService->getChannelServers($this->tedId);
        $newChannels = $arrayName = array();
        foreach ($tedsChannel as $tChannel) {
            $isHas = false;
            foreach ($currentServers as $aChannel) {
                if($tChannel["Name"]==$aChannel["Name"]){
                    $isHas = true;
                }
            }
            if($isHas==false){
                $newChannels[] = $tChannel;
            }
            $isHas = false;
        }

        foreach ($newChannels as $nChannel) {
            echo $this->channelService->insertServer($this->tedId,$nChannel["Name"],$nChannel["Link"]);
        }

    }
}
$rss = new RSSFeed();
$rss->AutoUpdateFeedData();
?>
